public class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
this.next = null;
}
}
連結:https://leetcode.com/problems/reverse-linked-list/description/
class Solution {
private ListNode rev(ListNode current,ListNode prev)
{
if (current == null) return prev;
ListNode temp = current.next;
current.next = prev;
return rev(temp,current);
}
public ListNode reverseList(ListNode head) {
return rev(head,null);
}
}
連結:https://leetcode.com/problems/reverse-linked-list/description/
class Solution {
private void mergelist(ListNode curr,ListNode list1,ListNode list2)
{
if (list1 == null)
{
curr.next = list2;
return ;
}
else if(list2 == null)
{
curr.next = list1;
return;
}
else if (list1.val <= list2.val)
{
curr.next = list1;
list1 = list1.next;
}
else
{
curr.next = list2;
list2 = list2.next;
}
curr = curr.next;
mergelist(curr,list1,list2);
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode root = new ListNode(0);
ListNode prev = root;
mergelist(prev,list1,list2);
return root.next;
}
}